package com.consumer;

import com.qingcheng.dao.OrderMapper;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WxPayService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class OrderTimeOutConsumer implements MessageListener {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPayService wxPayService;
    /**
     * 超时未支付订单，订单状态确认
     * @param message
     */
    @Override
    public void onMessage(Message message) {
        ///获取订单号
        String orderNumber = new String(message.getBody());
        //根据订单号查询业务订单状态
        System.out.println(orderNumber);
        Order order = orderMapper.selectByPrimaryKey(orderNumber);
        if (order.getPayStatus()!="1"){
//            查询微信状态
            Map payStatus = wxPayService.queryPayStatus(orderNumber);
            String result_code = (String)payStatus.get("result_code");
            if (result_code.equals("SUCCESS")){//若状态为成功,实现补偿操作
                order.setPayStatus("1");
                orderMapper.updateByPrimaryKey(order);//更新订单
            }else {//订单状态为未支付
                wxPayService.shutDownOrder(orderNumber);
                orderService.shutDownOrder(orderNumber);//更新订单
            }
        }
    }
}
